package t901

import "fmt"

type StockSpanner struct {
	items []int
}

func Constructor() StockSpanner {
	return StockSpanner{}
}

func (s *StockSpanner) Next(price int) int {
	cnt := 1
	if len(s.items) == 0 {
		s.items = append(s.items, price)
		return cnt
	}

	top := s.items[len(s.items)-1]
	if top > price {
		s.items = append(s.items, price)
		return cnt
	} else {
		// price >= top
		tmpStack := make([]int, 0)
		for {
			if len(s.items) == 0 {
				for i := len(tmpStack) - 1; i >= 0; i-- {
					s.items = append(s.items, tmpStack[i])
				}
				s.items = append(s.items, price)
				break
			}
			el := s.items[len(s.items)-1]
			s.items = s.items[:len(s.items)-1]
			tmpStack = append(tmpStack, el)
			if price >= el {
				cnt++
			} else {
				for i := len(tmpStack) - 1; i >= 0; i-- {
					s.items = append(s.items, tmpStack[i])
				}
				s.items = append(s.items, price)
				break
			}
		}
	}

	return cnt
}

func ttt() bool {

	obj := Constructor()

	fmt.Printf("%d\n", obj.Next(10))
	fmt.Printf("%d\n", obj.Next(20))
	fmt.Printf("%d\n", obj.Next(30))
	fmt.Printf("%d\n", obj.Next(50))
	fmt.Printf("%d\n", obj.Next(40))
	fmt.Printf("%d\n", obj.Next(25))
	fmt.Printf("%d\n", obj.Next(15))

	return true
}

/**
 * Your StockSpanner object will be instantiated and called as such:
 * obj := Constructor();
 * param_1 := obj.Next(price);
 */
